package com.zcyh.exam.controller.admin.exam;


import com.zcyh.exam.controller.BaseController;
import com.zcyh.exam.model.common.Result;
import com.zcyh.exam.model.entity.QuestionType;
import com.zcyh.exam.service.education.QuestionTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 试题类型接口
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/11/19 10:45
 */
@RestController
@RequestMapping("/system/questionType")
@Slf4j
public class QuestionTypeController extends BaseController {

    @Autowired
    private QuestionTypeService questionTypeService;
    @GetMapping
    public Result getAll() {
        return Result.success(questionTypeService.list());
    }
    /**
     * 添加或修改试题
     * @return
     */
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody @Validated QuestionType questionType) {
        return Result.success(questionTypeService.saveOrUpdate(questionType));
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable String id) {
        QuestionType questionType = questionTypeService.getById(id);
        if(questionType.getNum()!=0){
            Result.fail(500,"类型已被使用");
        }
        return Result.success(questionTypeService.deleteById(id));
    }


}
